﻿using System;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sustainalytics.DynamicsCRMGateway.Entities;


namespace Sustainalytics.DynamicsCRMGateway.Tests
{
    [TestClass()]
    public class CRMSimpleInterfaceTests
    {

        private string cnstr = @"Url=https://sustainalytics.crm4.dynamics.com;Username=client.platform@sustainalytics.com;Password=M46iaHerm3s21!";

        [TestMethod()]
        public void ReadTest()
        {

            var projections = new Expression<Func<Account, object>>[] { x => x.Id };
            var filters = new WhereClause<Account, object>[]
            {
                new WhereClause<Account, object>(x => x.RelationshipType, WhereClauseOperator.Equal , (int?)AccountRelationshipType.Client),
                new WhereClause<Account, object>(x => x.RelationshipType, WhereClauseOperator.Equal , (int?)AccountRelationshipType.Demo),
                new WhereClause<Account, object>(x => x.RelationshipType, WhereClauseOperator.Equal , (int?)AccountRelationshipType.Prospect),
                
            };
            using (var reader = new CRMSimpleReader<Account>(cnstr, 10, projections, filters))
            {
                var dic =  reader.ReadNextPage().ToDictionary(x=>x.Id);

                foreach (var e in reader.ReadNextPage())
                    dic.Add(e.Id, e);

                foreach (var e in reader.ReadNextPage())
                    dic.Add(e.Id, e);

                Assert.AreEqual(30, dic.Count);
            }
        }


        [TestMethod()]
        public void LargeReadTest()
        {
            
            var projections = new Expression<Func<Company, object>>[] { x => x.Id, x => x.CapitalIqId };
            using (var reader = new CRMSimpleReader<Company>(cnstr, 1000, projections, null))
            {

                var dic = reader.ReadNextPage().ToDictionary(x => x.Id);

                while(true) 
                {
                    var data = reader.ReadNextPage();
                    if (data == null)
                        break;
                    
                    foreach (var e in data)
                        dic.Add(e.Id, e);
                } 


                Assert.IsTrue(dic.Count > 5000);


            }
        }

       
    }
}
